package com.bird.config;

import lombok.Data;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @data 2021/9/12 16:56
 * @author: bird
 * @description: 生产者配置 自动创建 队列
 */
@Data
@Configuration
public class RabbitmqProductConfig {

    // 物料<==>渠道 交换机
    public String MATERIAL_CHANNEL_EXCHANGE = "material.channel.exchange";

    // 物料<==>渠道 队列
    public String MATERIAL_CHANNEL_QUEUE = "material.channel.queue";

    // 物料<==>渠道 路由key
    public String MATERIAL_CHANNEL_KEY = "material.channel.key";


    // 广播交换机
    @Bean
    public TopicExchange createExchange() {
        return new TopicExchange(MATERIAL_CHANNEL_EXCHANGE);
    }

    //队列
    @Bean
    public Queue createQueue() {
        return new Queue(MATERIAL_CHANNEL_QUEUE);
    }

    @Bean
    public Binding bindingExchange() {
        return BindingBuilder.bind(createQueue()).to(createExchange()).with(MATERIAL_CHANNEL_KEY);
    }


    // 压缩消息体
    @Bean
    public Jackson2JsonMessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }


    @Autowired
    private AmqpAdmin amqpAdmin;

    @Bean
    public void create() {
        amqpAdmin.declareBinding(bindingExchange());
    }
}
